import sys
import os
import shutil
import fnmatch

RequiredModa = .29

def getTxtLineCountAndLengths(path) :
    lengths = {}
    count = 0
    text_file = None

    try:
        text_file = open(path, "r")

        while True :
            line = text_file.readline()
            if line :
                count += 1
                length = len(line)
                if length in lengths.keys() :
                    lengths[length] += 1
                else : 
                    lengths[length] = 1
            else :
                break;
    finally:
        if text_file :
            text_file.close()

    return (count, lengths)

def getModaAndItsPart(path):
    count_lengths = getTxtLineCountAndLengths(path)
    count = count_lengths[0]
    lengths = count_lengths[1]
    lenKeys = lengths.keys()
    keysCount = len(lenKeys)
    if keysCount :
        theKey = -1
        maxCount = max(lengths.values())
        for k,v in lengths.iteritems() :
            if v == maxCount :
                theKey = k
        return (theKey, float(maxCount)/count)
    else:
        return (0,0)
    

def readTxtFile(path, allign) :
    out =''
    text_file = None

    try:
        text_file = open(path, "r")

        while True :
            line = text_file.readline()
            if line :
                length = len(line)
#                if length > 0 :
#                    print line, length, ord(line[length-2]), ord(line[length-1])
                if length == allign :
                    line = line[0:length-3]
                out += line
            else :
                break;
    finally:
        if text_file :
            text_file.close()
            
    return out

def convert(inPath, outPath) :
    moda_part = getModaAndItsPart(inPath)
    if(moda_part[1]>RequiredModa):
        out = readTxtFile(inPath, moda_part[0])
        out_text_file = open(outPath, "w")
        out_text_file.write(out)
        out_text_file.close()
    else :
        print 'moda', moda_part[0], 'part only', moda_part[1]
    
if os.path.isfile(sys.argv[1]) :
    outIsFile = sys.argv.count == 3

    outFileName = str()
    if outIsFile :
        outFileName = sys.argv[2]
    else :
        outFileName = sys.argv[1]
    convert(sys.argv[1], outFileName)

else :
    matches = []
    for root, dirnames, filenames in os.walk(sys.argv[1]):
      for filename in fnmatch.filter(filenames, '*.TXT'):
          print filename
          filename = os.path.join(root, filename)
          convert(filename)
      for filename in fnmatch.filter(filenames, '*.txt'):
          print filename
          filename = os.path.join(root, filename)
          convert(filename)
